package com.yyge.common.controller;

import cn.hutool.core.util.IdUtil;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.yyge.common.constans.RedisConstans;
import com.yyge.common.vo.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Desc 验证码操作
 * @Author: admin
 * @Date: 2023/06/29/15:50
 */
@RestController
@RequestMapping("/api")
@Api(tags = "验证码API")
public class CaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;



    @ApiImplicitParams( value =
        {
            @ApiImplicitParam(name = "width",value = "宽度",defaultValue = "150"),
            @ApiImplicitParam(name = "height",value = "宽度",defaultValue = "50"),
            @ApiImplicitParam(name = "len",value = "长度",defaultValue = "2"),
            @ApiImplicitParam(name = "type",value = "验证码类型(airth/gif/png)",defaultValue = "airth")
        }
    )
    @ApiOperation(value = "获取验证码" )
    @GetMapping(value = {"/captcha","front/captcha"} )
    public R<Map<String,Object>>  captcha(@RequestParam(value = "width",required = false,defaultValue = "150") int width,
                                          @RequestParam(value = "height",required = false,defaultValue = "50")int height,
                                          @RequestParam(value = "len",required = false,defaultValue = "2")int len,
                                          @RequestParam(value = "type",required = false,defaultValue = "airth")String type){

        Captcha  captcha = new SpecCaptcha(150, 50,2);
        //简单工厂设计模式
        if(type.equals("airth")){
            captcha= new ArithmeticCaptcha(width, height,len);
        }else if(type.equals("png")){
            captcha=  new SpecCaptcha(width, height,len);
        }else if(type.equals("gif")){
            captcha=  new GifCaptcha(width, height,len);
        }

        //得到验证码的内容
        System.out.println("获得验证码内容");
        System.out.println(captcha.text());
        String text= captcha.text();
        String imageStr=captcha.toBase64();
        //存储到redis中
        String UUID= IdUtil.fastUUID().toString();
        String redisKey= RedisConstans.getRedisKey(RedisConstans.CAPTCHA_KEY,UUID);
        //5分钟过期
        this.redisTemplate.opsForValue().set(redisKey, text, RedisConstans.CAPTCHA_EXPIRED_MINUTE, TimeUnit.MINUTES);

        //返回给前端
        Map<String,Object> dataMap=new HashMap<>();
        dataMap.put("uuid",UUID);
        dataMap.put("image",imageStr);

        return R.okHasData(dataMap);
    }

}
